package cn.phonevane.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import cn.phonevane.base.BaseAction;
import cn.phonevane.model.TTopic;
import cn.phonevane.service.TopicService;
import cn.phonevane.utils.CheckFileType;
import cn.phonevane.utils.SystemConstant;

public class TopicAction extends BaseAction{
	
	private TopicService topicService;
	
	
	//上传文件域对象
	private File[] uploads;
	//上传文件名
	private String[] uploadFileNames;
	//上传文件类型
	private String[] uploadContentTypes;
	//注入专题图片保存路径
	private String saveTopicImagePath;
	
	
	/**
	 * 把源文件对象复制成目标文件对象
	 */
	private static void copy(File src,File dst){
		InputStream in=null;
		OutputStream out=null;
		try{
		in=new BufferedInputStream(new FileInputStream(src),SystemConstant.BUFFER_SIZE); 
		out=new BufferedOutputStream(new FileOutputStream(dst),SystemConstant.BUFFER_SIZE);
		byte[] buffer= new byte[SystemConstant.BUFFER_SIZE];
		int len=0;
		while((len=in.read(buffer))>0){
			out.write(buffer, 0, len);
		}
		}catch(Exception e){
			logger.info("upload failed!",e);
		}finally{
			if(in!=null){
				try{
				in.close();
				in=null;
				}catch(IOException e){
					logger.info("in close failed!",e);
				}
			}
			if(out!=null){
				try{
				out.close();
				out=null;
				}catch(IOException e){
					logger.info("out close failed!",e);
				}
			}
		}
	}
	
	public String issue(){
		String title=httpServletRequest.getParameter("title");
		String keywords=httpServletRequest.getParameter("keywords");
		String strStatus=httpServletRequest.getParameter("status");
		String username=httpServletRequest.getParameter("username");
		String describe=httpServletRequest.getParameter("describe");
		if(title.equals("")||keywords.equals("")||strStatus.equals("")
				||username.equals("")||describe.equals("")){
			return ERROR;
			
		}else{
			TTopic tTopic=new TTopic();
			File [] strFiles=this.getUpload();
			String imageUrl="0";
			if(strFiles!=null){
				SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmmss");
				String newFileName=null,times=null,dstPath=null;
				times=sf.format(new Date());
				for(int i=0;i<strFiles.length;i++){
					newFileName=times+i+CheckFileType.getExtention(this.getUploadFileName()[i]);
					 dstPath=ServletActionContext.getServletContext()
		        		.getRealPath(this.getSaveTopicImagePath())
		        		+"/"+newFileName;	
					 imageUrl=this.getSaveTopicImagePath()+"/"+newFileName;
					 File dstFile=new File(dstPath);
					 copy(strFiles[i], dstFile);
				}
			}
			int status=Integer.parseInt(strStatus);
			tTopic.setTopicTitle(title);
			tTopic.setUsername(username);
			tTopic.setTopicKeywords(keywords);
			tTopic.setTopicDescribe(describe);
			tTopic.setTopicImageUrl(imageUrl);
			tTopic.setTopicStatus(status);
			tTopic.setTopicCreateTime(new Date());
			boolean sign=this.topicService.add(tTopic);
			if(!sign){
				return ERROR;
			}	
		}
		
		return SUCCESS;
	}
	

    public File[] getUpload() {
		
		return this.uploads;
	} 
	public void setUpload(File[] upload) {
		this.uploads = upload; 
	} 
	 
	public String[] getUploadFileName() { 
		return this.uploadFileNames;
	} 
	public void setUploadFileName(String[] uploadFileName) { 
		this.uploadFileNames = uploadFileName; 
	} 
	 
	public String[] getUploadContentType() { 
		return this.uploadContentTypes; 
	} 
	public void setUploadContentType(String[] uploadContentType) { 
		this.uploadContentTypes = uploadContentType; 
	}

	public String getSaveTopicImagePath() {
		return saveTopicImagePath;
	}

	public void setSaveTopicImagePath(String saveTopicImagePath) {
		this.saveTopicImagePath = saveTopicImagePath;
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}
	
	

}
